var bath = baseConfig.basePath;

$(function(){
	$(".accounts1").focus();
	
	//获取焦点，清除提示信息
	$('.clear-msg').on('focus',function(){
		var spanNode = $(this).parent().find('.box');
		$(spanNode).html('<font color="red"></font>');
	})
	
	var accountFlag = false;
	$('#accountCode').on('blur',function(){
		var data = this.id+'='+this.value;
		if($(".accounts1").val() == "") {
			$(".box1").html('<font color="red">请输入正确的账号!</font>');
			/*$(".accounts1").focus();*/
			accountFlag = false;
		}else{
			//校验账号：部署环境前不开放
			/*customUtils.ajaxByDefault('post',data,bath+'account/checkAccount',function(result){
				if(result.flag == true){
					$(".box1").html('<font color="green">帐号可用</font>');
					accountFlag = true;
				}else{
					$(".box1").html('<font color="red">帐号不可用</font>');
					accountFlag = false;
				}
			});*/
		}
	})
	
	var flag = false;
	//手机号码格式
	$('#phoneNum').on('input changeproperty',function(){
		var phone = this.value;
		var reg = customUtils.isPhone(phone);
		if(!reg){
			$(".box2").html("<font color='red'>手机号码不正确!</font>");
			//错误的手机号码，无法获取验证码
			flag = false;
		}else{
			$(".box2").html('<font color="red"></font>');
			$('#tempAC').val(phone);
			//当手机号码正确后，能获取验证码
			flag = true;
		}
	});
	
	
	var hasVerityCode = false;
	//获取验证码
	$('#verityCodeBtn').click(function(){
		var idNode = this.id;
		if(!flag){
			$(".box2").html("<font color='red'>手机号码不正确!</font>");
			$(this).text('获取验证码');
		}else{
			var urlPath = bath+'account/sendVerityCode';
			var data = 'phoneNum='+$('#phoneNum').val()
			customUtils.ajaxByDefault('post',data,urlPath,function(data){
				console.info(data);
				if(data.flag == '1'){
					changeVerityBtn(idNode,120);
					$(".box2").html('<font color="red"></font>');
					hasVerityCode = true;
				}else{
					$(".box2").html('<font color="red">'+data.msg+'</font>');
				}
			});
		}
	})
	
	var saveFlag = false;
	//校验验证码
	$('#verityCode').on('blur',function(){
		//当有验证码时，才能校验
		if(hasVerityCode){
			if(this.length<4 && this.val() != null){
				$(".box3").html("<font color='red'>验证码有误</font>");
			}else{
				var data = 'verityCode='+$('#verityCode').val();
				var urlPath = bath+'account/verityCode';
				customUtils.ajaxByDefault('post',data,urlPath,function(data){
					console.info(data);
					if(data.flag == '1'){
						$(".box3").html("<font color='red'></font>");
						saveFlag = true;
					}else{
						$(".box3").html("<font color='red'>验证码有误</font>");
					}
				});
			}
		}
	});

	$('#saveBtn').click(function(){
		$('#verityCode').blur();
		//TODO 校验账号：accountFlag
		
		if(saveFlag){
			var type=customUtils.getReq().type;
			console.info(type);
			switch (type) {
				case 'admin'  :  $('#type').val('0'); 	break;
				case 'client' :  $('#type').val('1'); 	break;
				default	:        $('#type').val('2'); 	break;
			}
			var formData = $('.form-account').serialize();
			var urlPath = bath+'account/save';
			customUtils.ajaxByDefault('post',formData,urlPath,function(data){
				window.location.href = bath+data.page;
				console.info(data);
			});
		}else{
			$('#verityCode').focus();
		}
	});
});